package gsp420.math;
import java.awt.Graphics;
/**
 * <pre>
 * P0
 * V
 * +---------+
 * |         |
 * |         |
 * +---------+
 *           ^
 *          p1
 * </pre>
 * @author michael.vaganov
 */
public class RectF
{
	/** top-left point */
	public V2F p0 = new V2F();
	/** bottom-right point */
	public V2F p1 = new V2F();
	public RectF(){}

	public RectF(float x0, float y0, float x1, float y1){
		set(x0, y0, x1, y1);
	}
	public void set(float x0, float y0, float x1, float y1){
		p0.set(x0, y0);	p1.set(x1, y1);
	}
	
	public void translate(V2F t){
		p0.add(t);
		p1.add(t);
	}
	
	public float getWidth(){return p1.x-p0.x;}
	public float getHeight(){return p1.y-p0.y;}
	public float getMinX(){return p0.x;}
	public float getMinY(){return p0.y;}
	public float getMaxX(){return p1.x;}
	public float getMaxY(){return p1.y;}
	/** g.{@link Graphics#drawRect(int, int, int, int)} */
	public void draw(Graphics g)
	{
		g.drawRect((int)p0.x, (int)p0.y, (int)getWidth(), (int)getHeight());
	}
	/** @return true if this point is inside. (p >= p0 && p < p1) */
	public boolean isInside(V2F p){
		return p.x >= p0.x && p.y >= p0.y
			&& p.x < p1.x && p.y < p1.y;
	}
	/**
	 * if the rectangle is not valid, according to {@link #isValid()}, this will not work
	 */
	public boolean isColliding(RectF b){
		return 
			!( b.getMaxX() < getMinX()
			|| b.getMaxY() < getMinY()
			|| b.getMinX() > getMaxX()
			|| b.getMinY() > getMaxY());
	}
	/** @return if this rectangle is an actual rectangle, with a valid width and height */
	public boolean isValid(){
		return getMinX() < getMaxX()
			&& getMinY() < getMaxY();
	}
}